$().ready(function() {
	$('.profile-page').livequery(function() {
		lastVariant = '';
		$('a#make-dogovor-link').fancybox({
			 helpers : {
			        overlay : {
			            opacity: 0.73
			        }
			 },
			scrolling: 'no',
			onStart: function() {
				this.content.html();
			}
		});
		$('.radio').click(function() {
			if($(this).val() == 'yes' && lastVariant != 'yes') {
				$('#add-content-1, #add-content-2').hide();
				$('#add-content-1').show();
				lastVariant = 'yes';
			}
			else if($(this).val() == 'no' && lastVariant != 'no') {
				$('#add-content-1, #add-content-2').hide('');
				$('#add-content-2').show();
				lastVariant = 'no';
			}
			$.fancybox.toggle();
		});
		
		$(function(){
			$('.field').fieldClear();
			$('.mandatory').hideErrors();
		});
		$('#contract-form').submit(function() {
			form = $(this);
			if(form.fullValidate()) {
				$.ajax({
					dataType: 'json',
					url: W_AJAX + 'contract/add',
					type: 'POST',
					data: {
						ID_master : $('#numer').attr('v'),
						contract_name : $('#Contract_contract_name',form).val(),
						contract_description : $('#Contract_contract_description',form).val(),
						first_name : $('#Contract_first_name',form).val(),
						last_name : $('#Contract_last_name',form).val(),
						middle_name : $('#Contract_middle_name',form).val(),
						deadline : $('#Contract_deadline',form).val(),
						city : $('#Contract_city',form).val()
					},
					success: function(data) {
						if (data.result == 'OK') {
							url = W_ABSPATH + 'profile/contracts/' + data.id;
							location.href = url;
						}
					}	
				});
			}
			else $.fancybox.reposition();
			return false;
		});
	});
});